/*****************************************************************************
*        描述 :
*             文件描述
*        CLR版本:            3.5
*        创建者 :            蒋 光
*        个人域名:           jiangguang.net.cn
*        命名空间名称:       ECP.DataAccess
*        文件名:             BaseModulePermissionDao.cs
*        创建系统时间:       2012/11/2 19:06:59
*        创建年份:           2012
*
/*****************************************************************************/

namespace ECP.DataAccess
{
    using ECP.DataAccess.Utilities;
    using ECP.Model;
    using ECP.Utilities;

    public class BaseModulePermissionDao : BaseDao
    {
        public BaseModulePermissionDao()
        {
            base.CurrentTableName = BaseResourcePermissionTable.TableName;
        }

        public BaseModulePermissionDao(IDbHelper dbHelper)
            : this()
        {
            base.DbHelper = dbHelper;
        }

        public BaseModulePermissionDao(IDbHelper dbHelper, BaseUserInfo userInfo)
            : this(dbHelper)
        {
            base.UserInfo = userInfo;
        }

        public string Add(string moduleID, string permissionID)
        {
            return this.Add(string.Empty, moduleID, permissionID);
        }

        public int Add(string moduleID, string[] permissionIDs)
        {
            int num = 0;
            string[] strArray = BaseSequenceDao.Instance.GetBatchSequence(base.DbHelper, BaseResourcePermissionTable.TableName, permissionIDs.Length);
            for (int i = 0; i < permissionIDs.Length; i++)
            {
                this.Add(strArray[i], moduleID, permissionIDs[i]);
                num++;
            }
            return num;
        }

        public int Add(string[] moduleIDs, string permissionID)
        {
            int num = 0;
            string[] strArray = BaseSequenceDao.Instance.GetBatchSequence(base.DbHelper, BaseResourcePermissionTable.TableName, moduleIDs.Length);
            for (int i = 0; i < moduleIDs.Length; i++)
            {
                this.Add(strArray[i], moduleIDs[i], permissionID);
                num++;
            }
            return num;
        }

        public int Add(string[] moduleIDs, string[] permissionIDs)
        {
            int num = 0;
            string[] strArray = BaseSequenceDao.Instance.GetBatchSequence(base.DbHelper, BaseResourcePermissionTable.TableName, moduleIDs.Length * permissionIDs.Length);
            for (int i = 0; i < moduleIDs.Length; i++)
            {
                int index = 0;
                while (i < permissionIDs.Length)
                {
                    this.Add(strArray[(i * permissionIDs.Length) + index], moduleIDs[i], permissionIDs[index]);
                    num++;
                    i++;
                }
            }
            return num;
        }

        public string Add(string id, string moduleID, string permissionID)
        {
            string str = string.Empty;
            string[] names = new string[3];
            string[] values = new string[3];
            names[0] = BaseResourcePermissionTable.FieldResourceCategory;
            values[0] = "Module";
            names[1] = BaseResourcePermissionTable.FieldResourceID;
            values[1] = moduleID;
            names[2] = BaseResourcePermissionTable.FieldPermissionID;
            values[2] = permissionID;
            if (!this.Exists(names, values))
            {
                BaseResourcePermissionEntity resourcePermission = new BaseResourcePermissionEntity
                {
                    ID = id,
                    ResourceID = moduleID,
                    ResourceCategory = "Module",
                    PermissionID = permissionID
                };
                str = this.AddEntity(resourcePermission);
            }
            return str;
        }

        public string AddEntity(BaseResourcePermissionEntity resourcePermission)
        {
            string iD = resourcePermission.ID;
            if (string.IsNullOrEmpty(iD))
            {
                iD = BaseSequenceDao.Instance.GetSequence(base.DbHelper, BaseResourcePermissionTable.TableName);
            }
            SQLBuilder builder = new SQLBuilder(base.DbHelper);
            builder.BeginInsert(BaseResourcePermissionTable.TableName);
            builder.SetValue(BaseResourcePermissionTable.FieldID, iD);
            builder.SetValue(BaseResourcePermissionTable.FieldResourceID, resourcePermission.ResourceID);
            builder.SetValue(BaseResourcePermissionTable.FieldResourceCategory, resourcePermission.ResourceCategory);
            builder.SetValue(BaseResourcePermissionTable.FieldCreateUserID, base.UserInfo.ID);
            builder.SetValue(BaseResourcePermissionTable.FieldPermissionID, resourcePermission.PermissionID);
            builder.SetDBNow(BaseResourcePermissionTable.FieldCreateDate);
            if (builder.EndInsert() <= 0)
            {
                return string.Empty;
            }
            return iD;
        }

        public int Delete(string[] moduleIDs, string permissionID)
        {
            int num = 0;
            for (int i = 0; i < moduleIDs.Length; i++)
            {
                num += this.Delete(moduleIDs[i], permissionID);
            }
            return num;
        }

        public int Delete(string moduleID, string permissionID)
        {
            string[] names = new string[3];
            string[] strArray2 = new string[3];
            names[0] = BaseResourcePermissionTable.FieldResourceCategory;
            strArray2[0] = "Module";
            names[1] = BaseResourcePermissionTable.FieldResourceID;
            strArray2[1] = moduleID;
            names[2] = BaseResourcePermissionTable.FieldPermissionID;
            strArray2[2] = permissionID;
            return this.BatchDelete(names, (object[])strArray2);
        }

        public int Delete(string[] moduleIDs, string[] permissionIDs)
        {
            int num = 0;
            for (int i = 0; i < moduleIDs.Length; i++)
            {
                int index = 0;
                while (i < permissionIDs.Length)
                {
                    num += this.Delete(moduleIDs[i], permissionIDs[index]);
                    i++;
                }
            }
            return num;
        }

        public int Delete(string moduleID, string[] permissionIDs)
        {
            int num = 0;
            for (int i = 0; i < permissionIDs.Length; i++)
            {
                num += this.Delete(moduleID, permissionIDs[i]);
            }
            return num;
        }

        public string[] GetPermissionIDs(string moduleID)
        {
            string[] names = new string[2];
            string[] values = new string[2];
            names[0] = BaseResourcePermissionTable.FieldResourceCategory;
            values[0] = "Module";
            names[1] = BaseResourcePermissionTable.FieldResourceID;
            values[1] = moduleID;
            return BaseBusinessLogic.FieldToArray(this.GetDT(names, values), BaseResourcePermissionTable.FieldPermissionID);
        }
    }
}